<template>
<div class="app-container hn-product-list-container">
  <div class="search-list">
    <div class="filter-container search-bar">
      <search-box :value.sync="listQuery.goodsName" placeholder="商品名称" @onSearch="handleFilter" @onReset="reset">
      </search-box>
      <div class="tool-oper">
        <el-button size="hn-small" class="filter-item el-button--hn-lightblue" round @click="onLookSelectedProducts()">已选商品（{{selectProducts.length}}）</el-button>
        <el-button size="hn-small" class="filter-item el-button--hn-lightblue" round @click="onCancel()">取消</el-button>
        <el-button size="hn-small" class="filter-item el-button--hn-lightblue" type="primary" @click="onSure" round>确定</el-button>
      </div>
    </div>
  </div>
  <draggable-table ref="dt" :loading="listLoading" :data="list" :header="tableHeader" :option="tableOption">
  </draggable-table>
  <div class="pagination-container">
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.pageNum" :page-sizes="[10, 12, 15, 20, 30]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
  <el-dialog class="hn-dialog" :title="dialogTitle" :visible.sync="dialogVisible">
    <div class="hn-product-qrcode">
      <div class="" ref="qrCode">
        <qrcode-vue className="text-center hn-print-qrcode" :value="productQrCode" :size="200"></qrcode-vue>
        <div class="hn-print-desc">
          {{dialogTitle}}
        </div>
      </div>
      <el-button size="hn-small" class="filter-item el-button--hn-lightblue" @click="onPrint()" round>打印二维码</el-button>
    </div>
  </el-dialog>
  <el-dialog title="已选商品" class="hn-dialog" :visible.sync="dialogSPVisible">
    <div class="hn-notimportane-table" style="margin-bottom: 60px;">
      <el-table
        :data="selectProducts"
        height="330"
        stripe
        row-class-name="hn-notimportane-table-body-row"
        ref="et"
        header-row-class-name="hn-notimportane-table-header-row"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="产品名称"
          width="130">
        </el-table-column>
        <el-table-column
          prop="code"
          label="货号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="brand"
          label="品牌">
        </el-table-column>
        <el-table-column
          prop="spec"
          label="规格">
        </el-table-column>
        <el-table-column
          prop="secondCode"
          width="120"
          label="第二项目号">
        </el-table-column>
        <el-table-column
          prop="num"
          label="数量">
        </el-table-column>
        <el-table-column
          fixed
          fixed="right"
          align="center"
          width="80"
          label="操作">
          <template slot-scope="scope">
            <el-button type='text' size='small' @click="onSelectedDel(scope.row, scope.$index)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="hn-dialog-footer clearfix">
      <el-button size="hn-small" class="fr el-button--hn-lightblue" type="primary" round @click="dialogSPVisible = false">
        确定
      </el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import draggableTable from '@/components/DraggableTable/index'
import searchBox from '@/components/SearchBox'
import { mapGetters } from 'vuex'
import { listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods, setSeriesId } from "@/api/goods/goods";

export default {
  name: "product-selector",
  props: ['visible', 'isComponent'],
  components: {
    draggableTable,
    searchBox
  },
  data() {
    return {
      list: [],
      status: '',
      saleStatus: [{
        code: '1',
        value: '是'
      },{
        code: '0',
        value: '否'
      }],
      total: 0,
      listLoading: true,
      dialogSPVisible: false,
      selectProducts: [],
      dialogTitle: '商品二维码',
      productQrCode: '',
      dialogVisible: false,
      tableOption: {
        hasSelection: true,
        noOperation: true,
        border: true,
        minHeight: 400,
        search: this.search,
        handleSelectionChange: this.handleSelectionChange,
        handleSelectionBoxChange: this.handleSelectionBoxChange
      },
      tableHeader: [],
      listQuery: this.initQuery()
    }
  },
  computed: {
    ...mapGetters([
      'imgServerUrl',
      'mcuId'
    ])
  },
  methods: {
    handleSelectionBoxChange(val, row) {
      row.active = !row.active
      if (row.active) {
        let isHas = false
        this.selectProducts.forEach((subItem, index) => {
          if (subItem.code === row.code) {
            isHas = true
          }
        })
        if (!isHas) {
          this.selectProducts.push(row)
        }
      } else {
        this.selectProducts.forEach((subItem, index) => {
          if (subItem.code === row.code) {
            this.selectProducts.splice(index, 1)
          }
        })
      }
    },
    handleSelectionChange(val) {
      this.selectProducts = val
      if (val) {
        val.forEach(item => {
          let isHas = false
          this.selectProducts.forEach(subItem => {
            if (subItem.code === item.code) {
              isHas = true
            }
          })
          if (!isHas) {
            this.selectProducts.push(item)
          }
        })
      }
    },
    onSure() {
      this.$emit('onCancel')
      this.$emit('onSelect', this.selectProducts)
    },
    onLookSelectedProducts() {
      this.dialogSPVisible = true
    },
    onSelectedDel(row, index) {
      this.selectProducts.splice(index, 1)
      this.$refs.dt.$refs.table.clearSelection()
    },
    onCancel() {
      this.$emit('onCancel')
    },
    goBack() {
      this.$store.dispatch('delVisitedViews', this.$route)
      this.$router.go(-1)
    },
    onPrint(e) {
      this.$print(this.$refs.qrCode)
    },
    initQuery() {
      return {
        pageNum: 1,
        pageSize: 15,
        goodsName: undefined,
        isOnSale: undefined,
        catId: undefined,
        catName: undefined,
        goodsSn: undefined,
        seriesId: undefined
      }
    },
    getProducts() {
      this.listLoading = true;
      listGoods(this.listQuery).then(response => {
        if (response && response.code === 200) {
          if (response.rows) { 
            const arr = []
            response.rows.forEach(item => {
              const product = {
                id: item.id,
                goodsName: item.goodsName,
                goodsImg: item.goodsImg,
                isOnSale: item.isOnSale,
                price: item.price,
                sort: item.sort,
                catId: item.catId,
                catName: item.catName,
                goodsSn: item.goodsSn,
                costPrice: item.costPrice,
                seriesId: item.seriesId,
              }
              arr.push(product)
            })
            this.list = arr
          }
          if (response.total) {
            this.total = response.total;
          }
        }
        this.listLoading = false;
      });
    },
    reset() {
      this.listQuery.goodsName = ''
      this.getProducts()
    },
    search(val, key) {
      if (key === 'cid') {
        this.listQuery.cid = val
      } else if (key === 'goodsName') {
        this.listQuery.goodsName = val
      } else if (key === 'isOnSale') {
        this.listQuery.isOnSale = val
      } else if (key === 'catId') {
        this.listQuery.catId = val
      } else if (key === 'catName') {
        this.listQuery.catName = val
      } else if (key === 'goodsSn') {
        this.listQuery.goodsSn = val
      } else if (key === 'seriesId') {
        this.listQuery.seriesId = val
      }
      this.listQuery.pageNum = 1;
      this.getProducts();
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getProducts()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getProducts()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val - 1
      this.getProducts()
    }
  },
  created() {
    this.getProducts()
    this.tableHeader = [{
      prop: 'xid',
      label: '序号',
      type: 'index',
      width: '50'
    }, {
      prop: 'goodsImg',
      label: '图片',
      width: '100',
      disabled: true,
      isImg: true
    }, {
      prop: 'id',
      label: '唯一ID',
      disabled: true,
      width: '280'
    }, {
      prop: 'goodsName',
      key: 'goodsName',
      label: '商品名称',
      width: '180'
    }, {
      prop: 'isOnSale',
      key: 'isOnSale',
      label: '是否上架',
      width: '120',
      renderHeader: (h) => {
        return (
          <el-select
            class='header-input header-input-select'
            value={this.status}
            on-change={value => {
              this.listQuery.pageNum = 1
              this.listQuery.isOnSale = value
              this.status = value
              this.getProducts()
            }}
            placeholder={'请选择'}
          >
            {(this.saleStatus || []).map((item, index) => (
              <el-option
                key={index}
                label={item.value}
                value={item.code}
              />
            ))}
          </el-select>
        )
      }
    }, {
      prop: 'price',
      key: 'price',
      label: '平台基准价格',
      disabled: true,
      width: '90'
    }, {
      prop: 'sort',
      key: 'sort',
      label: '商品排序',
      disabled: true,
      width: '90'
    }, {
      prop: 'catId',
      key: 'catId',
      label: '商品分类ID',
      width: '280'
    }, {
      prop: 'catName',
      key: 'catName',
      label: '商品分类名称',
      width: '90'
    }, {
      prop: 'costPrice',
      key: 'costPrice',
      label: '商店成本价格',
      disabled: true,
      width: '90'
    }, {
      prop: 'seriesId',
      key: 'seriesId',
      label: '商品系列ID',
      width: '280'
    }]
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.hn-product-list-container {
    position: relative;
    background: #fff;
    padding: 35px;
}
.filter-item.search-bar_input input {
    height: 24px;
    line-height: 24px;
    font-size: 13px;
    border: 1px solid #5181b0;
    border-radius: 5px 0 0 5px;
}

.el-button-group.fixed-place.search-btn_list {
    margin-left: -14px;
    margin-top: -1px;
}

.search-bar_btn1 {
    padding: 0 20px;
    height: 24px;
    line-height: 24px;
    background: #5181b0;
    border: 1px solid #5181b0;
    border-radius: 0;
}

.search-bar_btn2 {
    padding: 0 20px;
    height: 24px;
    line-height: 24px;
    border: 1px solid #5181b0;
    border-radius: 0;
    color: #5181b0;
    border-radius: 0 5px 5px 0;
}

.pagination-container {
    background: #5181b0;
    color: #fff;
    padding: 11px 30px;
    border-radius: 0 0 4px 4px;
    text-align: right;
}

.pagination-container .el-pagination__jump {
    color: #fff;
}

.car-box {
    display: flex;
    img {
        vertical-align: middle;
        margin-top: 2px;
        margin-left: 15px;
        width: 24px;
        height: 24px;
    }
}

.hn-product-qrcode {
    text-align: center;
    button {
        margin-top: 20px;
    }
}

.hn-print-desc {
    display: none;
    text-align: center;
}

@media print {
    .hn-print-qrcode {
        padding: 50px 0 15px;
    }
    .hn-print-desc {
        display: block;
    }
}
</style>
